import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import type { useState, info } from "./../../types/loginType";

const initialState: useState = {
    token: "",
    info: {
        userId: 0,
        username: "",
        account: "",
        phone: "",
        userRole: null,
        permissions: null
    },
    publicKey:""
}

const userInfo = createSlice({
    name: "userInfo",
    initialState,
    reducers: {
        setUserInfo: (state, action: PayloadAction<info>) => {
            state.info = Object.assign(state.info, action.payload);
        },
        clearUserInfo: (state) => {
            state.info = Object.assign(state.info, {
                userId: 0,
                username: "",
                account: "",
                phone: "",
                userRole: null,
                permissions: null
            });
        },
        setToken: (state, action: PayloadAction<string>) => {
            state.token = action.payload;
        },
        clearToken: (state) => {
            state.token = "";
        },
        setPublicKey: (state, action: PayloadAction<string>) => {
            state.publicKey = action.payload;
        },
    }
});


export const { setToken, clearToken, clearUserInfo, setUserInfo ,setPublicKey} = userInfo.actions;

export default userInfo.reducer;

